/*
 * @Author: 张小平
 * @Date:   2025-05-01
 * @Email:  qqshuqian@163.com
 * @Copyright © 2024 aseyun. All rights reserved.
 */
/**
 * 查看和办理页面的按钮权限查询
 * @param buttonDisplay
 * @param http
 * @param message
 * @param pageKey
 * @param modeldata
 */

import request from '@/framework-core/axios';
import website from '@/framework-core/config/website';
export function getButtonAuthority(buttonDisplay, http, message, pageKey, modeldata) {
  for (let key in buttonDisplay) {
    buttonDisplay[key].display = false
    buttonDisplay[key].info = []
  }

  let url
  if (pageKey === 'DO') {
    // 办理页面的按钮事件
    url = '/flow-task-change/do-task-form-button-authority?taskId=' + modeldata.taskId
  } else {
    url = '/flow-task-change/view-form-button-authority?processInstanceId=' + modeldata.processInstanceId
  }

  request.get(website.baseCodeApiUrl+url).then(res => {
    if (res.code === 200) {
      if (res.data) {
        for (let key in res.data) {
          buttonDisplay[key] = res.data[key]
        }
      }
    } else {
      message.error(res.msg)
    }
  })
  // .catch(err => {
  //   if (err.msg) { message.error(err.msg) } else { message.error(err.message) }
  // })
}

/**
 * 获取查看和办理列表上方的下拉选择框数据
 * @param http
 * @param message
 * @param involvedMe
 * @param conf
 */
export function getProcessDefinitionKeyList(http, message, involvedMe, conf) {
  conf.mainTable.table.columns[0].dicData = []
  request.get(website.baseCodeApiUrl+'/flow-ext/process-definition-list', { params: { 'involvedMe': involvedMe }}).then(res => {
    if (res.code === 200) {
      if (res.data && res.data.length > 0) {
        for (let i = 0; i < res.data.length; i++) {
          conf.mainTable.table.columns[0].dicData.push({ label: res.data[i].name, value: res.data[i].key })
        }
      } else { return }
    } else {
      message.error(res.msg)
    }
  })
  // .catch(err => {
  //   if (err.msg) { message.error(err.msg) } else { message.error(err.message) }
  // })
}

/**
 * 终止
 * @param http
 * @param message
 * @param modeldata
 * @param buttonDialog
 * @param authButtonDisplay
 */
export function terminateProcess(http, message, modeldata, buttonDialog, authButtonDisplay) {
  request.delete(website.baseCodeApiUrl+'/flow-task-change/delete-process-instance', { params: { 'processInstanceId': modeldata.processInstanceId, 'deleteReason': buttonDialog.reason }}).then(res => {
    if (res.code === 200) {
      buttonDialog.dialogVisible = false
      getButtonAuthority(authButtonDisplay, http, message, 'VIEW', modeldata)
      message.info('终止' + res.msg)
    }
  })
  // .catch(res => {
  //   message.error(res.msg)
  // })
}

/**
 * 退回上一步
 * @param http
 * @param message
 * @param modeldata
 * @param buttonDialog
 * @param authDisplay
 */
export function revokePreTask(http, message, modeldata, buttonDialog, authDisplay) {
  if (modeldata.processInstanceId && authDisplay.revokePreTask.info) {
    let params = '?processInstanceId=' + modeldata.processInstanceId
    if (buttonDialog.reason && buttonDialog.reason.length > 0) {
      params += '&revokePreTaskReason=' + buttonDialog.reason
    }
    /* /!*return*!/
        return*/
    request.post(website.baseCodeApiUrl+'/flow-task-change/revoke-per-task' + params, authDisplay.revokePreTask.info).then(res => {
      if (res.code === 200) {
        message.info(res.msg)
        buttonDialog.dialogVisible = false
        getButtonAuthority(authDisplay, http, message, 'VIEW', modeldata)
      } else {
        message.error(res.msg)
      }
    })
    // .catch(err => {
    //   if (err.msg) { message.error(err.msg) } else { message.error(err.message) }
    // })
  }
}

/**
 * 转办
 * @param http
 * @param message
 * @param modeldata
 * @param buttonDialog
 * @param authDisplay
 */
export function turnTask(http, message, modeldata, buttonDialog, authDisplay) {
  let params = '?taskId=' + modeldata.taskId + '&acceptUserId=' + buttonDialog.acceptUserId
  if (buttonDialog.reason) { params += '&turnTaskReason=' + buttonDialog.reason }
  request.put(website.baseCodeApiUrl+'/flow-task-change/turn-task' + params).then(res => {
    if (res.code === 200) {
      message.info(res.msg)
      buttonDialog.dialogVisible = false
      getButtonAuthority(authDisplay, http, message, 'DO', modeldata)
    } else {
      message.error(res.msg)
    }
  })
  // .catch(err => {
  //   if (err.msg) { message.error(err.msg) } else { message.error(err.message) }
  // })
}

/**
 * 委派
 * @param http
 * @param message
 * @param modeldata
 * @param buttonDialog
 * @param authDisplay
 */
export function delegateTask(http, message, modeldata, buttonDialog, authDisplay) {
  let params = '?taskId=' + modeldata.taskId + '&acceptUserId=' + buttonDialog.acceptUserId
  if (buttonDialog.reason) { params += '&delegateTaskReason=' + buttonDialog.reason }
  request.post(website.baseCodeApiUrl+'/flow-task-change/delegate-task' + params).then(res => {
    if (res.code === 200) {
      message.info(res.msg)
      buttonDialog.dialogVisible = false
      getButtonAuthority(authDisplay, http, message, 'DO', modeldata)
    } else {
      message.error(res.msg)
    }
  })
  // .catch(err => {
  //   if (err.msg) { message.error(err.msg) } else { message.error(err.message) }
  // })
}

/**
 * 取消委派
 * @param http
 * @param message
 * @param modeldata
 * @param buttonDialog
 * @param authDisplay
 */
export function undoDelegateTask(http, message, modeldata, buttonDialog, authDisplay) {
  let params = '?taskId=' + modeldata.taskId
  if (buttonDialog.reason) { params += '&undoDelegateTaskReason=' + buttonDialog.reason }
  request.delete(website.baseCodeApiUrl+'/flow-task-change/undo-delegate-task' + params).then(res => {
    if (res.code === 200) {
      message.info(res.msg)
      buttonDialog.dialogVisible = false
      getButtonAuthority(authDisplay, http, message, 'DO', modeldata)
    } else {
      message.error(res.msg)
    }
  })
  // .catch(err => {
  //   if (err.msg) { message.error(err.msg) } else { message.error(err.message) }
  // })
}

/**
 * 加签功能
 * @param http
 * @param message
 * @param modeldata
 * @param buttonDialog
 * @param authDisplay
 * @param isDoTask
 */
export function addSign(http, message, modeldata, buttonDialog, authDisplay, isDoTask) {
  let params = '?processInstanceId=' + modeldata.processInstanceId + '&acceptUserId=' + buttonDialog.acceptUserId + '&taskDefKey=' + authDisplay.addTask.info
  if (buttonDialog.reason) { params += '&addTaskReason=' + buttonDialog.reason }
  request.post(website.baseCodeApiUrl+'/flow-task-change/add-task' + params).then(res => {
    if (res.code === 200) {
      message.info(res.data.msg)
      buttonDialog.dialogVisible = false
      if (isDoTask) { getButtonAuthority(authDisplay, http, message, 'DO', modeldata) } else { getButtonAuthority(authDisplay, http, message, 'VIEW', modeldata) }
    } else {
      message.error(res.msg)
    }
  })
  // .catch(err => {
  //   if (err.msg) { message.error(err.msg) } else { message.error(err.message) }
  // })
}

/**
 * 减签功能
 * @param http
 * @param message
 * @param modeldata
 * @param buttonDialog
 * @param authDisplay
 * @param isDoTask
 */
export function deleteSign(http, message, modeldata, buttonDialog, authDisplay, isDoTask) {
  let params = '?taskId=' + buttonDialog.acceptUserName
  if (buttonDialog.reason) { params += '&addTaskReason=' + buttonDialog.reason }
  request.delete(website.baseCodeApiUrl+'/flow-task-change/delete-task' + params).then(res => {
    if (res.code === 200) {
      message.info(res.msg)
      buttonDialog.dialogVisible = false
      if (isDoTask) { getButtonAuthority(authDisplay, http, message, 'DO', modeldata) } else { getButtonAuthority(authDisplay, http, message, 'VIEW', modeldata) }
    } else {
      message.error(res.msg)
    }
  })
  // .catch(err => {
  //   if (err.msg) { message.error(err.msg) } else { message.error(err.message) }
  // })
}

/**
 * 初始化查看和办理页面的按钮
 * @param buttonDialog
 * @param key
 * @param label
 * @param title
 * @param acceptLabel
 * @param isAccept
 */
export function initButtonDialog(buttonDialog, key, label, title, acceptLabel, isAccept) {
  buttonDialog.key = key
  buttonDialog.label = label
  buttonDialog.title = title
  buttonDialog.acceptLabel = acceptLabel
  buttonDialog.isAccept = isAccept
  buttonDialog.dialogVisible = true
  buttonDialog.info = ''
  buttonDialog.reason = ''
  buttonDialog.acceptUserName = ''
  if (key !== 'deleteTask') { buttonDialog.acceptUserId = '' }
}
